<script type="text/javascript" src="js/jquery.alphanumeric.js"></script> 
<script type="text/javascript" src="js/jquery.formatCurrency-1.4.0.min.js"></script>
<script type="text/javascript">
	$(function(){
		$('.number').live('blur',function(){
			$(this).formatCurrency({symbol: ''});
		});
		number();
		$("input[name=add_debit]").live('click',function(){
			$(this).parent().parent().before('<tr><td class="dr1">' + $('.debit_ddl').html() + ' <a href="#" class="icon icon-delete delete"></a></td><td><input type="text" name="price[dr][]" value="" class="number" style="width:100px;" /></td><td></td></tr>');
			number();
		})
		$("input[name=add_credit]").click(function(){
			$(this).parent().parent().before('<tr><td class="cr1">' + $('.credit_ddl').html() + ' <a href="#" class="icon icon-delete delete"></a></td><td></td><td><input type="text" name="price[cr][]" value="" class="number" style="width:100px;"  /></td></tr>');
			number();
		})
		
		$("a.delete").live('click',function(){
			if (confirm('ยืนยันการลบ')) {
				var a = $(this);
				a.parent().parent().remove();
			}
			return false;
		})
		
		$("input[name='price[dr][]'],input[name='price[cr][]']").live('keyup',function(event){
			var dr = 0;
			var cr = 0;
			$("input[name='price[dr][]'],input[name='price[cr][]']").each(function(){
				if ($(this).val() != "") {
					if ($(this).attr('name') == "price[dr][]") {
						dr += parseFloat($(this).val().replace(/\,/g, ""), 2);
					}
					else {
						cr += parseFloat($(this).val().replace(/\,/g, ""), 2);
					}
				}
			})
			$("input[name=debit_sum]").val(dr.toFixed(2)).prev().text(dr).formatCurrency({symbol: '', negativeFormat: '-%s%n'});
			$("input[name=credit_sum]").val(cr.toFixed(2)).prev().text(cr).formatCurrency({symbol: '', negativeFormat: '-%s%n'});
			number(); 
		})
		$("input[name='price[dr][]'],input[name='price[cr][]']").trigger('keyup');
		
		
		$("#frmMain").validate({
			rules: 
			{
				voucher_date: 
				{ 
					required: true
				},
				number: 
				{ 
					required: true
				},
				credit_sum:
				{
					equalTo: "#debit"
				}
			},
			errorPlacement: function(error, element) 
			{
     			if (element.attr("name") == "credit_sum")
      				error.appendTo("#error");
     			else
      				error.insertAfter(element);
  		 	},
			messages:
			{
				voucher_date: 
				{ 
					required: "กรุณากรอกวันเดือนปีที่ลงบัญชีค่ะ"
				},
				number: 
				{ 
					required: "กรุณากรอกเลขที่ค่ะ"
				},
				credit_sum:
				{
					equalTo: "กรุณากรอกเดบิตและเครดิตให้เท่ากันค่ะ"
				}
			},
			submitHandler: function(form) {
				var drItem = 0;
				var crItem = 0;
	      		$("input[name='price[dr][]']").each(function(){
					if (($(this).val() != "")&&($(this).val() != 0)) {
						drItem++;
					}
				})
				$("input[name='price[cr][]']").each(function(){
					if (($(this).val() != "")&&($(this).val() != 0)) {
						crItem++;
					}
				})
				if((drItem>1)&&(crItem>1))
				{
					$("#error").html('<label class="error">กรุณากรอกเดบิตหรือเครดิตเพียง 1 รายการเท่านั้นค่ะ</label>');
					return false;
				}
				else if((drItem==0)||(crItem==0))
				{
					$("#error").html('<label class="error">กรุณากรอกเดบิตและเครดิตอย่างน้อย 1 รายการค่ะ</label>');
					return false;
				}
				else
				{
					form.submit();
				}
   			}
		});
	})
function number()
{
	$('.number').numeric({allow:"."});
}
</script>
<h1><?php echo $fund['name'] ?></h1>
<ul id="breadcrumb">
	<li><span>ใบสำคัญการลงบัญชี</span></li>	
	<li>ปรับปรุง</li>		
</ul>
<div id="content">
<form id="frmMain" action="voucher/save" method="post" >
<table class="form">
	<tr><th>วันเดือนปีที่ลงบัญชี</th><td><input type="text" name="voucher_date" value="<?php echo mysql_to_date($rs['voucher_date'],TRUE) ?>" <?php echo fixDate($fund['id']) ?> class="datepicker" /></td></tr>
	<tr><th>เลขที่</th><td><input type="text" name="number" value="<?php echo $rs['number'] ?>" /></td></tr>
	<tr><th>รายการการลงบัญชี</th><td class="voucher_match"></td></tr>
	<tr>
		<th colspan="2" style="background:#EEE;">
		<div id="voucher_match">
			<table class="tbform">
				<tr>
					<th style="width:100%;">ชื่อบัญชี</th>
					<th style="width:100px;">เดบิต</th>
					<th style="width:100px;">เครดิต</th>
				</tr>
				<?php if(isset($debits)): ?>
				<?php foreach ($debits as $debit): ?>
				<tr>
					<td class="dr1"><?php echo form_dropdown('account_id[dr][]',get_option('ID','NAME','ACCOUNTS',' FUND_ID = '.$fund['id']),$debit['account_id']) ?> <a href="#" class="icon icon-delete delete"></a></td>
					<td>
						 <input type="text" name="price[dr][]" value="<?php echo $debit['price'] ?>" class="number" style="width:100px;" />
						 <input type="hidden" name="detail_id[dr][]" value="<?php echo $debit['id'] ?>" />
					</td>
					<td></td>
				</tr>
				<?php endforeach; ?>
				<?php else: ?>
				<tr>
					<td class="dr1"><?php echo form_dropdown('account_id[dr][]',get_option('ID','NAME','ACCOUNTS',' FUND_ID = '.$fund['id'])) ?> <a href="#" class="icon icon-delete delete"></a></td>
					<td>
						<input type="text" name="price[dr][]" value="" class="number" style="width:100px;" />  
					</td>
					<td></td>
				</tr>
				<?php endif; ?>
				<tr><td colspan="3"><input type="button" name="add_debit" value="เพิ่มเดบิต" class="button" /></td></tr>
				<?php if(isset($credits)): ?>
				<?php foreach ($credits as $credit): ?>
				<tr>
					<td class="cr1"><?php echo form_dropdown('account_id[cr][]',get_option('ID','NAME','ACCOUNTS',' FUND_ID = '.$fund['id']),$credit['account_id']) ?> <a href="#" class="icon icon-delete  delete"></a> </td>
					<td></td>
					<td>
						<input type="text" name="price[cr][]" value="<?php echo $credit['price'] ?>" class="number" style="width:100px;" /> 
						<input type="hidden" name="detail_id[cr][]" value="<?php echo $credit['id'] ?>" /> 
						
					</td>
				</tr>
				<?php endforeach; ?>
				<?php else: ?>
				<tr>
					<td class="cr1"><?php echo form_dropdown('account_id[cr][]',get_option('ID','NAME','ACCOUNTS',' FUND_ID = '.$fund['id'])) ?> <a href="#" class="icon icon-delete delete"></td>
					<td></td>
					<td>
						<input type="text" name="price[cr][]" value="" class="number" style="width:100px;" />  
						</a>
					</td>
				</tr>
				<?php endif; ?>
				<tr><td colspan="3"><input type="button" name="add_credit" value="เพิ่มเครดิต" class="button" style="width:100px;" /></td></tr>
				<tr>
					<th id="error"></td>
					
					<th><span></span><input id="debit" type="hidden" name="debit_sum" value=""  class="number" /></th>
					<th><span></span><input type="hidden" name="credit_sum" value=""  class="number" /></th>
				</tr>
			</table>
	<tr>
		<th>คำอธิบาย</th>
		<td>
			<textarea name="description" cols="100" rows="6" ><?php echo $rs['description'] ?></textarea>
		</td>
	</tr>
	<tr>
		<th></th>
		<td>
			<input type="submit" value="<?php echo lang('btn_submit')?>" class="button" />
			<input type="button" value="<?php echo lang('btn_back')?>" class="button" onclick="window.location = 'voucher/index/<?php echo $fund['id'] ?>?mode=<?php echo $_GET['mode'] ?>'" />
			<input type="hidden" name="id" value="<?php echo $rs['id'] ?>" />
			<input type="hidden" name="fund_id" value="<?php echo $fund['id'] ?>" />
			<input type="hidden" name="voucher_type_id" value="3" />
			<input type="hidden" name="mode" value="<?php echo $_GET['mode'] ?>" />
		</td>
	</tr>
</table>
</form>
</div>

<div class="debit_ddl" style="display:none;">
<?php echo form_dropdown('account_id[dr][]',get_option('ID','NAME','ACCOUNTS',' FUND_ID = '.$fund['id'])) ?>
</div>
<div class="credit_ddl" style="display:none;">
<?php echo form_dropdown('account_id[cr][]',get_option('ID','NAME','ACCOUNTS',' FUND_ID = '.$fund['id'])) ?>
</div>
